package org.game.msgBuilder;

import java.util.List;

import org.constant.PlayerAttrConstant;
import org.game.db.bean.Formation;
import org.game.db.bean.Player;
import org.game.db.bean.PlayerData;
import org.game.db.proxy.bean.PlayerDataProxy;
import org.game.db.vo.PlayerDataVO.DiscreteVO;
import org.game.protobuf.s2c.S2CPlayerMsg;
import org.game.protobuf.s2c.S2CPlayerMsg.FormationInfo;
import org.game.protobuf.s2c.S2CPlayerMsg.FormationInfoList;
import org.game.protobuf.s2c.S2CPlayerMsg.Language;
import org.game.protobuf.s2c.S2CPlayerMsg.PlayerAttr;
import org.game.protobuf.s2c.S2CPlayerMsg.PlayerAttrKey;
import org.game.protobuf.s2c.S2CPlayerMsg.PlayerInfo;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;

/**
 * @author : DengYing
 * @CreateDate : 2017年8月15日 下午4:21:17
 * @Description ：Please describe this document
 */
public class PlayerMsgBuilder {

	/** 创建玩家信息 */
	public static PlayerInfo createPlayerInfo(Player player) {
		PlayerInfo.Builder playerBuilder = PlayerInfo.newBuilder();
		playerBuilder.setPid(player.getId());
		playerBuilder.setName(player.getName());
		playerBuilder.setLvl(player.getLevel());
		playerBuilder.setExp(player.getExp());
		playerBuilder.setVipLvl(player.getVipLevel());
		playerBuilder.setVipExp(player.getVipExp());
		playerBuilder.setLanguage(Language.ZH_CN);
		playerBuilder.setRemark(player.getRemark());
		
		PlayerData playerData = player.getPlayerDatasFkPlayerId();
		PlayerDataProxy playerDataProxy = playerData.getPlayerDataProxy();
		DiscreteVO discreteVO = playerDataProxy.getDiscreteVO();
		int helpFightHeroCid = discreteVO.getHelpFigntHeroCid(); 
		
		playerBuilder.setHelpFightHeroCid(helpFightHeroCid);
		playerBuilder.setIsFirstLogin(player.getPlayerProxy().isFirstLogin());
		if (discreteVO.getNewPlayerGuide() != null){
			playerBuilder.setClientDiscreteData(discreteVO.getNewPlayerGuide());
		}
		playerBuilder.setSettings(discreteVO.getSettings());

		// 玩家属性
		playerBuilder.addAttr(createPlayerAttr(PlayerAttrKey.FREE_RESET_ANGEL_COUNT, discreteVO.getResetAngelCount()));
		
		return playerBuilder.build();
	}
	
	/** 创建玩家信息 */
	public static PlayerInfo createPlayerAttrInfo(Player player) {
		PlayerInfo.Builder playerBuilder = PlayerInfo.newBuilder();
		
		PlayerData playerData = player.getPlayerDatasFkPlayerId();
		PlayerDataProxy playerDataProxy = playerData.getPlayerDataProxy();
		DiscreteVO discreteVO = playerDataProxy.getDiscreteVO();
		// 玩家属性
		playerBuilder.addAttr(createPlayerAttr(PlayerAttrKey.FREE_RESET_ANGEL_COUNT, discreteVO.getResetAngelCount()));
		
		return playerBuilder.build();
	}
	
	/** 创建阵型消息 */
	public static FormationInfo createFormationInfo(ChangeType changeType,Formation formation){
		FormationInfo.Builder builder =  FormationInfo.newBuilder();
		builder.setCt(changeType);
		builder.setType(formation.getType());
		builder.setStatus(formation.getStatus());
		List<Long> heroIds = formation.getStance();
		for (Long heroid : heroIds) {
			builder.addStance(Long.toString(heroid));
		}
		return builder.build();
	}
	
	/** 创建阵型列表消息 */
	public static FormationInfoList createFormationInfoList(ChangeType changeType,List<Formation> formations){
		FormationInfoList.Builder builder = FormationInfoList.newBuilder();
		for (Formation formation : formations) {
			builder.addFormations(createFormationInfo(changeType, formation));
		}
		return builder.build();
	}
	
	public static PlayerAttr createPlayerAttr(PlayerAttrKey key,int val){
		return PlayerAttr.newBuilder().setAttrKey(key).setAttrVal(val).build();
	}

	public static S2CPlayerMsg.RespBuyResources buildRespBuyResourcesMsg(int cid,int count){
		S2CPlayerMsg.RespBuyResources.Builder builder = S2CPlayerMsg.RespBuyResources.newBuilder();
		builder.setCid(cid).setCount(count);
		return builder.build();
	}
}
